/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Apr 8, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.plot.ruler;

import edu.psu.geovista.app.plot.axis.Range;

import java.awt.*;

public class LogRuler extends DfRuler{
      private Range originalTickRange;//

    public LogRuler() {
        this(Ruler.VERTICAL, 200, new Point(),new Range(100,1));
    }

    public LogRuler(Range range) {

         this.setTickRange(range);

    }

    public LogRuler(int orient, int length, Point op, Range range) {

        super(orient, length, op, range);
        this.originalTickRange = range;


    }
    protected void init(){
        super.init();
        this.originalTickRange =new Range();
    }
     protected void setup(int orient, int length, Point op, Range range) {
            this.length =length;//must set before set tickrange, since setTickRange() will calculate scale

            this.setTickRange(range);

            //ui=new
            this.setOrient(orient);

            this.setLocation(op);// location=op;

            //tickValues
            installUI();
        }

    public Range getTickOriginalRange() {
              return originalTickRange;
    }

    public void setTickRange(Range range) {


        Range trange = this.getTransformValue(range);
        boolean updated = this.setTickRange(trange.getMin() ,trange.getMax());
         if(updated){
            this.pcs.firePropertyChange(Ruler.PROPERTY_TICKRANGE,null,trange);
         }
        this.setOriginalTickRange(range);
     }
    /**
     * This is the transformed tick values
      * @return
     */
    public float[] getTicksvalue() {
        return super.getTicksvalue();
    }
    public float getValueOf(float gv){
        float tv = super.getValueOf(gv);//transformed
        return getReverseTransformValue(tv);
    }

    public void setOriginalTickRange(Range originalTickRange) {
        this.originalTickRange = originalTickRange;
    }

     protected Range getTransformValue(Range range){
         float min = range.getMin();
         float max = range.getMax();

         if(min<1||max<1){
                // 
                new IllegalArgumentException(this.getClass().getName() +" is passed a value less than 1, won't be able to process.");

         }
         float tmin = this.getTransformValue(min);
         float tmax = this.getTransformValue(max);
         return new Range(tmax,tmin);

     }

     protected float getTransformValue(float v){
        /* float tv=0;
         if(v>0){
             tv=(float) Math.log(v);
         }
         else if(v==0){
             tv=0;
         }
         else{//<0
             tv=-((float) Math.log(-v));
         }*/
         return (float) Math.log(v);
     }
    /**
     *  given a transformed value, return original
     * @param v
     * @return
     */
     protected float getReverseTransformValue(float v){
         //Math.log10()
         /*float rv=0;
         if(v>0){
             rv= (float) Math.exp(v);
         }
         else if(v<0){
             rv= -(float) Math.exp(-v);
         }
         else{
             rv=0;
         }*/
         return (float) Math.exp(v);
     }
}
